from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader, RequestContext
from booktest.models import BookInfo  # 导入图书模型


# Create your views here.
# 1. 定义视图函数
# 2. url配置 建立url地址与视图关系

def my_rander(request, temp_path, context_dict):
    # return HttpResponse('老铁没毛病')

    # 使用模板文件
    # 1. 加载模板文件
    temp = loader.get_template(temp_path)
    # 2. 定义模板上下文：给模板文件传数据
    # context = RequestContext(request, context_dict)
    # 3. 模板渲染：产生标准的html内容 返回
    return HttpResponse(temp.render(context_dict))


def index(request):
    # return HttpResponse('老铁没毛病')
    return my_rander(request, 'booktest/index.html',
                     {'content': 'hello world !', 'list': list(range(1, 10))})
    # return  my_rander(request,'booktest/index.html')


def index2(request):
    return HttpResponse('hello python')


def show_books(request):
    '''显示图书信息'''
    # 1. M 查护 图书表的信息
    books = BookInfo.objects.all()
    return render(request, 'booktest/show_books.html', {'books': books})


def detail(request, bid):
    ''' 查询英雄信息 '''
    book = BookInfo.objects.get(id=bid)
    heros = book.heroinfo_set.all()
    return render(request, 'booktest/detail.html', {'book': book, 'heros': heros})
